/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * RSOptics.cpp
 *
 *  Created on: 2010/03/07
 *      Author: Alexandre Pires ( c.alexandre.pires@gmail.com )
 */

#include "RSOptics.h"
#include "RSTechnique.h"
#include "RSProjectionTechnique.h"
#include <QList>
#include <QMatrix4x4>

namespace Realsphere {

    RSOptics::RSOptics () :
            RSElement ( RS_OPTICS_TAG )
    {
        mProjectionTechnique = NULL;
    }

    RSOptics::~RSOptics ()
    {
        mTechniques.clear ();
        mProjectionTechnique = NULL;
    }

    RSProjectionTechnique* RSOptics::ProjectionTechnique() const
    {
        return mProjectionTechnique;
    }

    RSTechnique* RSOptics::GetTechnique ( unsigned int rIndex ) const
    {
        return mTechniques[rIndex];
    }

    RSTechnique* RSOptics::GetTechnique ( const QString& rProfile ) const
    {

        RSTechniquesIterator iterator = RSTechniquesIterator ( mTechniques );

        while ( iterator.hasNext () )
        {

            RSTechnique* technique = iterator.next ();

            if ( technique->Profile () == rProfile )
                return technique;
        }

        return NULL;
    }

    int RSOptics::IndexOf ( RSTechnique* rTechnique )
    {
        return mTechniques.indexOf ( rTechnique );
    }

    unsigned int RSOptics::TechniquesSize ()
    {
        return mTechniques.size ();
    }

    RSTechniques RSOptics::Techniques () const
    {
        return mTechniques;
    }

    RSTechniquesIterator RSOptics::TechniquesIterator () const
    {
        return RSTechniquesIterator ( mTechniques );
    }

    bool RSOptics::IsRootElement() const {
        return false;
    }

    QMatrix4x4 RSOptics::ProjectionMatrix () const {

        QMatrix4x4 projection;

        if ( mProjectionTechnique == NULL )
            return projection;

        return mProjectionTechnique->ProjectionMatrix();
    }

    RSError RSOptics::SetProjectionTechnique( RSProjectionTechnique* rTechnique )
    {
        RSError error = AddTechnique ( rTechnique );
        return error;
    }

    RSError RSOptics::AddTechnique ( RSTechnique* rTechnique )
    {
        RSError error = AddChildElement ( rTechnique );
        return error;
    }

    RSError RSOptics::RemoveTechnique ( RSTechnique* rTechnique )
    {
        RSError error = RemoveChildElement ( rTechnique );
        return error;
    }

    RSError RSOptics::RemoveTechnique ( const QString& rProfile )
    {
        RSTechnique* technique = GetTechnique ( rProfile );
        RSError error = RemoveTechnique ( technique );
        return error;
    }

    RSError RSOptics::RemoveTechnique ( unsigned int rIndex )
    {
        RSTechnique* technique = GetTechnique ( rIndex );
        RSError error = RemoveTechnique ( technique );
        return error;
    }

    RSError RSOptics::BeforeAddChildElement ( RSElement* rChild )
    {
        RSError error = RSElement::BeforeAddChildElement ( rChild );

        if ( error == RS_INVALID_ELEMENT )
        {
            if ( rChild->InstanceOf ( RS_PROJECTION_TECHNIQUE_CLASS ) )
            {
                error = RS_NO_ERROR;
            }
            else if ( rChild->InstanceOf ( RS_TECHNIQUE_CLASS ) )
            {
                RSTechnique* technique = RSTechniqueCast(rChild);
                RSTechnique* profile_technique =
                        GetTechnique ( technique->Profile () );

                if ( profile_technique != NULL )
                    error = RS_PROFILE_TECHNIQUE_EXISTS;
                else
                    error = RS_NO_ERROR;
            }
        }

        return error;
    }

    bool RSOptics::AfterAddChildElement ( RSElement* rChild )
    {
        bool handled = RSElement::AfterAddChildElement ( rChild );

        if ( !handled )
        {
            if ( rChild->InstanceOf ( RS_PROJECTION_TECHNIQUE_CLASS ) )
            {
                if ( mProjectionTechnique != NULL )
                    RemoveChildElement ( mProjectionTechnique );

                mProjectionTechnique = RSProjectionTechniqueCast(rChild);
                connect ( mProjectionTechnique, SIGNAL ( TechniqueChanged() ), this, SIGNAL ( ProjectionChanged () ) );
                handled = true;

                emit ProjectionTechniqueAdded ( mProjectionTechnique );
            }
            else if ( rChild->InstanceOf ( RS_TECHNIQUE_CLASS ) )
            {
                RSTechnique* technique = RSTechniqueCast (rChild);
                mTechniques.append ( technique );
                handled = true;

                emit TechniqueAdded ( technique );
            }
        }

        return handled;
    }

    bool RSOptics::AfterRemoveChildElement ( RSElement* rChild )
    {
        bool handled = RSElement::AfterRemoveChildElement ( rChild );

        if ( !handled )
        {
            if ( rChild->InstanceOf ( RS_PROJECTION_TECHNIQUE_CLASS ) )
            {
                RSProjectionTechnique* projection_technique = RSProjectionTechniqueCast(rChild);

                disconnect ( mProjectionTechnique, SIGNAL ( TechniqueChanged() ), this, SIGNAL ( ProjectionChanged () ) );
                mProjectionTechnique = NULL;
                handled = true;

                emit ProjectionTechniqueRemoved( projection_technique );
            }
            else if ( rChild->InstanceOf ( RS_TECHNIQUE_CLASS ) )
            {
                RSTechnique* technique = RSTechniqueCommonCast (rChild);
                int technique_index = mTechniques.indexOf ( technique );
                mTechniques.removeAt ( technique_index );
                handled = true;

                emit TechniqueRemoved ( technique );
            }
        }

        return handled;
    }
}
